import req from '@/utils/request';
import * as URL from '@/constants/api';

export function getProductList(roleId: number): Promise<any> {
  return req({
    url: `${URL.PROJECT_LIST}/${roleId}`,
  })
    .then((res) => res.data.data)
    .catch(() => []);
}

export function getExampleList(): Promise<any> {
  return req({
    url: URL.EXAMPLE_LIST,
  })
    .then((res) => res.data.data)
    .catch(() => []);
}

export function postProduct(name: string): Promise<any> {
  return req({
    url: URL.PROJECT_LIST,
    method: 'post',
    data: {
      productName: name,
      roleId: 1,
    },
  })
    .then((res) => res.data.results)
    .catch(() => []);
}

export function deleteProduct(id: number): Promise<any> {
  return req({
    url: `${URL.PROJECT_LIST}/${id}`,
    method: 'delete',
  })
    .then((res) => res.data)
    .catch(() => []);
}

interface Person {
  id: number;
  productName: string;
}

export function putProduct(params: Person): Promise<any> {
  return req({
    url: `${URL.PROJECT_LIST}/${params.id}`,
    method: 'put',
    data: {
      productName: params.productName,
    },
  })
    .then((res) => res.data);
}

export function putProductStatus(params: any): Promise<any> {
  return req({
    url: `${URL.PROJECT_LIST}/${params.id}`,
    method: 'put',
    data: {
      status: params.status,
    },
  })
    .then((res) => res.data);
}

export function undateSave(id: number, params: object): Promise<any> {
  return req({
    url: `${URL.PROJECT_LIST}/${id}`,
    method: 'put',
    data: params,
  })
    .then((res) => res)
    .catch(() => []);
}

export function getComponetsList(id: number, roleId: number): Promise<any> {
  return req({
    url: `${URL.PROJECT_ORODUCT}/${id}`,
    method: 'get',
    params: {
      roleId,
    },
  })
    .then((res) => res.data.data)
    .catch(() => []);
}
